package cn.tedu.crm.user.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.tedu.crm.base.pojo.PageObject;
import cn.tedu.crm.base.response.JsonResult;
import cn.tedu.crm.dept.pojo.entity.Dept;
import cn.tedu.crm.user.pojo.dto.UserLoginParam;
import cn.tedu.crm.user.pojo.dto.UserModuleQuery;
import cn.tedu.crm.user.pojo.dto.UserModuleSaveParam;
import cn.tedu.crm.user.pojo.dto.UserQuery;
import cn.tedu.crm.user.pojo.vo.*;
import cn.tedu.crm.user.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("crm/user/login")
    public JsonResult login(@RequestBody UserLoginParam param) {
        UserLoginVO userLoginVO = userService.login(param);
        return JsonResult.success(userLoginVO);
    }
    @GetMapping("crm/user/select")
    public JsonResult select(UserQuery userQuery) {
        List<UserVO> list = userService.selectUserList(userQuery);
        return JsonResult.ok(list);
    }
    @GetMapping("sys/users/select")
    public JsonResult selectUserModule(UserModuleQuery userModuleQuery) {
        PageObject<UserModuleVO> pageObject = PageObject.page(
                userModuleQuery.getPageNum(), userModuleQuery.getPageSize(),
                ()->userService.selectUserModule(userModuleQuery));
        log.debug(":{}", pageObject);
        return JsonResult.ok(pageObject);
    }

    @PostMapping("sys/users/save")
    @SaCheckPermission("crm:system:user:add")
    public JsonResult save(@RequestBody UserModuleSaveParam userModuleSaveParam) {
        log.debug(":{}", userModuleSaveParam);
        userService.saveUserModule(userModuleSaveParam);
        return JsonResult.ok();
    }

    @SaCheckPermission("crm:system:user:updata")
    @GetMapping("sys/users/updata/select/{id}")
    public JsonResult selectUserById(@PathVariable Long id) {
        log.debug("修改用户id:{}", id);
        UserSelectVO userSelectVO = userService.selectUserById(id);
        return JsonResult.ok(userSelectVO);
    }

    @SaCheckPermission("crm:system:user:del")
    @DeleteMapping("sys/users/delete")
    public JsonResult deleteUser(@RequestParam List<Long> ids) {
        log.debug("删除用户：{}",ids);
        userService.deleteUser(ids);
        return JsonResult.ok();
    }

    @PatchMapping("sys/users/{userId}/resetPassword")
    public JsonResult resetPassword(@PathVariable Long userId) {
       userService.resetPassword(userId);
        return JsonResult.ok();
    }

    /**
     *
     * 根据部门id查询部门人数
     * @param query
     * @return
     */
    @GetMapping("crm/user/selectDeptCount")
    public JsonResult<Integer> selectDeptCount(UserQuery query){
        Integer peopleNum = userService.selectDeptCount(query);
        return JsonResult.ok(peopleNum);
    }
}
